﻿#include "GameManager.h"
#include "GamePlatform.h"


#include "_Global/GameCommon.h"


#include <QApplication>
#include <QDir>
#include <QProcess>
#include <QResource>
#include <QFont>
#include <QFontDatabase>
#include <QMessageBox>
#include <QQuickStyle>



#if defined(DYNAMICLIB_LIBRARY)
#  define DYNAMICLIBSHARED_EXPORT Q_DECL_EXPORT
#else
//#  define DYNAMICLIBSHARED_EXPORT Q_DECL_IMPORT
#  define DYNAMICLIBSHARED_EXPORT
#endif


extern "C" int DYNAMICLIBSHARED_EXPORT libMain(char *args)
{
    QApplication &app = *qApp;  //(static_cast<QApplication *>(QCoreApplication::instance()))
    QStringList strlistArguments = QString(args).trimmed().split(' ');
    qDebug() << "strlistArguments:" << strlistArguments;

    //鹰：
    QGuiApplication::setQuitOnLastWindowClosed(true);



    //启动程序前按平台初始化
    int nRet = initialization(app);
    if(nRet < 0)
        return nRet;



#if defined(Q_OS_WIN32)   //win
#ifdef QT_NO_DEBUG
    if(!_Platform::checkOne(L"Pleafles_Pets"))
    {
        qDebug() << "已经运行!";
        return -10;
    }

    if(strlistArguments.count() < 3 || QString("-s") != strlistArguments[1] || QString("Leamus") != strlistArguments[2])
    {
        //QProcess::startDetached("Updater.exe");
        //return -11;
    }
#endif
#endif



//载入字体
    QFont font;
    int fontId = QFontDatabase::addApplicationFont(FIXLOCALPATH_R("Media/Fonts/Font1.ttf"));
    if (fontId != -1)
    {
        QStringList strList(QFontDatabase::applicationFontFamilies(fontId));
        if (strList.count() > 0)
        {
            font.setFamily(strList.at(0));
            qDebug() << "字体载入成功:" << strList.at(0);
        }
    }
    else
        font.setFamily("黑体");

    app.setFont(font);
    //font.setPointSize(16);
    qDebug() << "字体:" << font.family();


    //QQuickStyle::setStyle("Material");



    //Q_INIT_RESOURCE
    //Q_CLEANUP_RESOURCE()
    //从rcc文件中载入qml文件和其他资源(可以载入多个,引用方式为qrc:/...)
    bool ret = QResource::registerResource(FIXLOCALPATH_R("QML.dat")) &
            QResource::registerResource(FIXLOCALPATH_R("Res.dat"));
    if(!ret)
    {
        QMessageBox::information(nullptr,"失败","载入资源文件失败!");
        qCritical()<<"载入资源文件失败!";
        return -100;
    }


    qDebug()<<"Main ThreadId:"<<QThread::currentThreadId();
    qDebug()<<"Sql Drivers:"<<QSqlDatabase::drivers();


    GameManager *manager = new GameManager;
    if(!manager->InitOnce())
        return -101;

    //程序退出时调用
    QObject::connect(&app, &QCoreApplication::aboutToQuit, [=]()
    {
        qDebug() << "aboutToQuit";
        manager->deleteLater();
        //if(g_CommonData.m_pMessageHandlerInfo != nullptr)
        //    delete g_CommonData.m_pMessageHandlerInfo;
    });

    return 0;
}



int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    //QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);   //绘图方式

    qputenv( "QSG_RENDER_LOOP", "basic" );  //单线程绘图,解决qml过快问题

    QApplication app(argc, argv);
#if defined(Q_OS_MACOS)
    //苹果默认工作目录是 ~(/Users/[User])，所以需要更改默认路径程序当前路径，否则相对目录无法使用
    //安卓的默认工作目录是/data/data/appName/files，但应用路径在/data/app/appName/lib下
    //win下是相同的
    QDir::setCurrent(QCoreApplication::applicationDirPath());
#endif


    QString args;
    while(argc > 0)
        args = args + argv[--argc] + " ";
    int nRet = libMain(args.toLatin1().data());
    if(nRet < 0)
        return nRet;


    return app.exec();
}
